package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.assemble.AdminAccessmbler;
import com.tarena.lbs.basic.web.service.AdminService;
import com.tarena.lbs.common.basic.converter.AdminConverter;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.dos.AdminDO;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import com.tarena.lbs.pojo.basic.vo.AreaVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminConverter adminConverter;
    @Autowired
    private AdminAccessmbler accessmbler;

    @ApiOperation("查询后台登录用户详情")
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> adminDetail() throws BusinessException{
        //1. 调用业务层,获取AdminBO
        AdminBO adminBO = adminService.adminDetail();
        log.debug("获取用户详细信息:出参:AdminBO {}",adminBO);
        //2.把AdminBO 转化为AdminVO
        AdminVO vo = null;
        if (adminBO!=null){
            vo = new AdminVO();
            BeanUtils.copyProperties(adminBO,vo);
        }
        return new Result<>(vo);
    }

    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> adminList(@RequestBody AdminQuery query) throws BusinessException{
        log.debug("获取商家登录管理员账户的分页信息 AdminQuery:{}",query);
        //封装一个分页
        query.setPageNo(1);
        query.setPageSize(10);
        //进行分页查询数据
        BasePageList<AdminBO> adminBo = adminService.adminList(query);
        //使用转化器,将AdminBo 和query装配成一个新的分页对象
        PageResult<AdminVO> adminVOPageResult = adminConverter.assemblePageVOS(adminBo, query);
        return new Result<>(adminVOPageResult);
    }

    @ApiOperation("新增商家账号信息接口")
    @PostMapping("/admin/basic/role/add")
    public Result<Void> addAdmin(@RequestBody AdminParam adminParam) throws BusinessException {
        log.debug("新增商家账号信息接口入参AdminParam:{}",adminParam);
        adminService.addAdminObj(adminParam);
        return Result.success();
    }

    @GetMapping("/admin/basic/area/tree")
    public Result<List<AreaVO>> frtchAreaVO(){
        log.debug("新增推文区域ID查询入口.....");
        List<AreaVO> stringListMap = accessmbler.assembleAreaTags();
        return new Result<>(stringListMap);
    }
}
